﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using EntityObjects;
using iTextSharp.text;
using iTextSharp.text.pdf;
using BAL.Controller;
using iTextSharp.text.html.simpleparser;

namespace PresentationLayer.View.StoreClerk
{
    public class CreatePDF
    {
        
        RaisePurchaseOrderControl control;
        static int count;
        public CreatePDF()
        {
            control = new RaisePurchaseOrderControl();

        }
        public void Create(PurchaseOrder po,string filename)
        {
            // Create a Document object 
            var document = new Document(PageSize.A4, 50, 50, 25, 25);
            
            // Create a new PdfWriter object, specifying the output stream
            var output = new MemoryStream();
            var writer = PdfWriter.GetInstance(document, output);
            string pdfPath = HttpContext.Current.Server.MapPath("PDFs");
            PdfWriter.GetInstance(document, new FileStream(pdfPath+"/"+filename+".pdf", FileMode.Create));
            // Open the Document for writing
            document.Open();
            // Read in the contents of the Receipt.htm file...
            string contents = File.ReadAllText(HttpContext.Current.Server.MapPath("template.htm"));            
            // Replace the placeholders with the user-specified text
            contents = contents.Replace("[poNo]", po.PONo);
            contents = contents.Replace("[supID]", po.SupplierId);
            contents = contents.Replace("[wantedDate]", po.WantedDate.ToShortDateString());
            contents = contents.Replace("[empID]", po.OrderedBy);
            contents = contents.Replace("[orderdDate]", po.OrderedOn.ToShortDateString());
            PdfPTable t = new PdfPTable(5);

            // Step 4: Parse the HTML string into a collection of elements...
            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
            document.Open();
            count = 0;
            createOrderDetailsTable(t,po.PoDetails_list);
            // Enumerate the elements, adding each one to the Document...
            foreach (var htmlElement in parsedHtmlElements)
            {
                count++;
                if (count == 6)
                {
                    document.Add(t);
                }
                document.Add(htmlElement as IElement);
            }
            document.Close();
        }

        private void createOrderDetailsTable(PdfPTable t,List<PurchaseOrderDetails> poDList)
        {           
            float[] widths = new float[] { 2f, 3f, 1f, 1f, 1f };
            t.SetWidths(widths);
            t.AddCell("Item Code");
            t.AddCell("Description");
            t.AddCell("Quantity");
            t.AddCell("Price");
            t.AddCell("Amount");
            foreach (PurchaseOrderDetails poD in poDList)
            {
                t.AddCell(poD.ItemId);
                t.AddCell(control.getDescription(poD.ItemId));
                t.AddCell(poD.OrderQty.ToString());
                t.AddCell(poD.Price.ToString());
                t.AddCell(poD.Amount.ToString());               
                }
            }
        }

    
}